// @ts-ignore
import Speech from 'speak-tts'
let speechSwitch: boolean = true;
let speech: any = undefined;
export const initSpeech = () => {
  speech = new Speech();
  if(speech.hasBrowserSupport()) { // 检测浏览器是否支持，returns a boolean
    console.log("语音引擎加载成功")
    speech.init({
      volume: 0.9, // 音量0-1
      lang: 'zh-CN', // 语言
      rate: 1, // 语速1正常语速，2倍语速就写2
      pitch:1, // 音调
      voice:'Microsoft Yaoyao - Chinese (Simplified, PRC)',//支持Microsoft Huihui - Chinese (Simplified, PRC),Microsoft Kangkang - Chinese (Simplified, PRC),Microsoft Yaoyao - Chinese (Simplified, PRC)
    }).then(data => {
      console.log('语音已准备好，声音可用', data);
    }).catch(e => {
      console.error('初始化时发生错误 : ', e);
    });
  }

  return speech;
}

export const setSpeechSwitch = (flag:boolean) => {
  speechSwitch = flag;
}
export const setSpeechVolume = (volume:number) => {
  speech.volume = volume / 10;
}

export const handleSpeak = (text:string) => {
  //speech.speaking
  if(speechSwitch && speech) {
    speech.speak({
      text: text, //这里使用文字或者i18n 都可以 看自己需求
      queue: false,
    })
      .then(() => {
        console.log('Success !');
      })
      .catch(e => {
        console.error('An error occurred :', e);
      });
  }
}
